package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.util.MaterialDialogUtils;
import com.house.rent.App;
import com.house.rent.R;
import com.house.rent.entity.HouseEntity;
import com.house.rent.ui.adapter.HouseAdapter;

import java.util.List;

import butterknife.BindView;

/**
 * @desc 我的收藏
 */
public class MyCollectionActivity extends BaseActivity {


    @BindView(R.id.refresh_collection)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.rcv_collection)
    RecyclerView mRecyclerView;

    HouseAdapter mAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, MyCollectionActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initTop() {
        setTitle("我的收藏");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter = new HouseAdapter(mContext));
    }

    @Override
    protected void initListeners() {
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            initData();
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            HouseDetailActivity.start(mContext, mAdapter.getData().get(position).getId());
        });
        mAdapter.setOnLoadMoreListener(null, mRecyclerView);
        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            //弹出是否取消收藏
            MaterialDialogUtils.show(mContext, "是否取消收藏", (dialog, which) ->
                    doCollectionReq(mAdapter.getData().get(position).getId()));
            return false;
        });
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        addParams("userId", App.getInstance().getUserEntity().getId());
        sendRequest("user-collect/collectList", HouseEntity.class);
    }

    /**
     * 请求收藏
     */
    private void doCollectionReq(long houseId) {
        addParams("houseInfoId", houseId);
        addParams("userId", App.getInstance().getUserEntity().getId());
        sendRequest("user-collect/cancel-collect", null);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof List) {
            List<HouseEntity> entities = (List<HouseEntity>) obj;
            mAdapter.setNewData(entities);
            mAdapter.loadMoreEnd();

            if (entities.isEmpty()) {
                mAdapter.setEmptyView(R.layout.layout_empty);
            }
        }
        if (reqId.equals("user-collect/cancel-collect")) {
            showMessage("取消收藏成功");
            initData();
        }
    }

    @Override
    public void showLoading(String message) {
        if (!mSwipeRefreshLayout.isRefreshing()) {
            super.showLoading(message);
        }
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }
}
